package com.penghaisoft.microwms.material.web.expose.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.ConRawCondDto;
import com.penghaisoft.microwms.common.dto.MesConRawReqDto;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IMesInterfaceService;
import com.penghaisoft.microwms.common.service.MesProperties;
import com.penghaisoft.microwms.common.wms.model.RawContractChangePlan;
import com.penghaisoft.microwms.common.wms.model.RawContractChangePlanSn;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IMaterialInsideService;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IRawContractChangePlanService;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IRawContractChangePlanSnService;

import lombok.extern.slf4j.Slf4j;

/** 
 * @Package com.penghaisoft.microwms.material.web.expose.controller
 * @ClassName: ExposeMesPhoneController
 * @Description: 原材料手机接口
 * @author luoteng
 * @date 2019年3月11日 上午10:56:32
 */
@RestController
@RequestMapping(value = "expose/mes/phone")
@Slf4j
public class ExposeMesPhoneController extends BaseController  {
	
	@Autowired
	private IMaterialInsideService iMaterialInsideService;
	
	@Autowired
	private IRawContractChangePlanSnService iRawContractChangePlanSnService;
	
	@Autowired
	private IRawContractChangePlanService iRawContractChangePlanService;
	
	@Autowired
	private IMesInterfaceService mesInterfaceService;
	@Resource
	private MesProperties mesProperties;
	
	@PostMapping("storage")
	public TransResult rawStorage(@RequestBody RawStorageSn rawStorageSn) {
		TransResult result = new TransResult();
		
		if(rawStorageSn.getContractNo()==null || rawStorageSn.getContractNo().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		MesConRawReqDto mesConRawRequired = new MesConRawReqDto();
		mesConRawRequired.setSalesCode(rawStorageSn.getContractNo());
		
		String url =  mesProperties.getUrl() + "/imes-provider/external/interfaces/contract/material";
		JSONObject handResp = mesInterfaceService.publicWSJson(url, MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mesConRawRequired));
		JSONArray listjson = handResp.getJSONObject("data").getJSONArray("list");
		
		ConRawCondDto conRawCondDto = new ConRawCondDto();
		conRawCondDto.setContractNo(rawStorageSn.getContractNo());
		List<MesConRawReqDto> list = iMaterialInsideService.findconRawRequired(conRawCondDto);
		
		List<MesConRawReqDto> mesList = new ArrayList<MesConRawReqDto>();
		if(list.size()>0){
			for(int i=0; i<list.size();i++){
				MesConRawReqDto conRawRequired = new MesConRawReqDto();
				conRawRequired.setNum(i);
				conRawRequired.setContractNo(list.get(i).getContractNo());
				conRawRequired.setModel(list.get(i).getModel());
				conRawRequired.setInAmount(list.get(i).getInAmount());
				conRawRequired.setOutAmount(list.get(i).getOutAmount());
				conRawRequired.setKcAmount(list.get(i).getKcAmount());
				conRawRequired.setJcAmount(list.get(i).getJcAmount());
				conRawRequired.setJrAmount(list.get(i).getJrAmount());
				conRawRequired.setUnit(list.get(i).getUnit());
				
				for(int j=0; j<listjson.size();j++){
					JSONObject temp = listjson.getJSONObject(j);
					if(list.get(i).getModel().equals(temp.getString("materialCode"))){
						conRawRequired.setDemand(new BigDecimal (temp.getString("demand")));
//						conRawRequired.setUnit(temp.getString("measureUnit"));
						conRawRequired.setOwner(temp.getString("owner"));
						break;
					}
				}
				
				mesList.add(conRawRequired);
			}
		}
		
		return success(mesList);
	}
	
	/**
	 * @Title: jy
	 * @Description: 原材料合同借用汇总详情
	 * @date 2019年3月12日 下午4:32:31
	 * @author luoteng
	 * @param rawContractChangePlanSn
	 * @return TransResult   
	 */
	@PostMapping("jy")
	public TransResult jy(@RequestBody RawContractChangePlanSn rawContractChangePlanSn) {
		TransResult result = new TransResult();
		
		if(rawContractChangePlanSn.getContractNoOld()==null || rawContractChangePlanSn.getContractNoOld().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		List<RawContractChangePlanSn> list = iRawContractChangePlanSnService.getJYHZ(rawContractChangePlanSn);
		
		return success(list);
	}
	
	/**
	 * @Title: jyInfo
	 * @Description: 原材料合同借用明细
	 * @date 2019年3月13日 下午1:27:12
	 * @author luoteng
	 * @param rawContractChangePlanSn
	 * @return TransResult   
	 */
	@PostMapping("jy/info")
	public TransResult jyInfo(@RequestBody RawContractChangePlanSn rawContractChangePlanSn) {
		TransResult result = new TransResult();
		
		if(rawContractChangePlanSn.getContractNoOld()==null || rawContractChangePlanSn.getContractNoOld().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		Pager<RawContractChangePlanSn> list = iRawContractChangePlanSnService.getJYHZInfo(rawContractChangePlanSn.getPage(), rawContractChangePlanSn.getRows(), rawContractChangePlanSn);
		
		return success(list);
	}
	
	/**
	 * @Title: jy
	 * @Description: 原材料合同借用汇总详情【改从主表汇总】
	 * @date 2019年3月20日 下午5:48:44
	 * @author luoteng
	 * @param rawContractChangePlan
	 * @return TransResult   
	 */
	@PostMapping("jy/new")
	public TransResult jyNew(@RequestBody RawContractChangePlan rawContractChangePlan) {
		TransResult result = new TransResult();
		
		if(rawContractChangePlan.getContractNoOld()==null || rawContractChangePlan.getContractNoOld().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		List<RawContractChangePlan> list = iRawContractChangePlanService.getJYHZ(rawContractChangePlan);
		
		return success(list);
	}
	
	/**
	 * @Title: jyInfo
	 * @Description: 原材料合同借用明细
	 * @date 2019年3月13日 下午1:27:12
	 * @author luoteng
	 * @param rawContractChangePlanSn
	 * @return TransResult   
	 */
	@PostMapping("jy/info/new")
	public TransResult jyInfoNew(@RequestBody RawContractChangePlan rawContractChangePlan) {
		TransResult result = new TransResult();
		
		if(rawContractChangePlan.getContractNoOld()==null || rawContractChangePlan.getContractNoOld().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		Pager<RawContractChangePlan> list = iRawContractChangePlanService.getJYHZInfo(rawContractChangePlan.getPage(), rawContractChangePlan.getRows(), rawContractChangePlan);
		
		return success(list);
	}
}
